-- 有两种特殊的掉落
-- 英雄：根据星级 阵营（可多个） 是否精英抽选
-- 神器：根据部位 等级 稀有度 阵营抽选

local HeroExcel = require("excel.hero")
local EquipExcel = require("excel.equip")
local ItemExcel = require("excel.item").item
local Grid = require("bag.Grid")
local SuipianLogic = require("bag.SuipianLogic")
local ItemDefine = require("bag.ItemDefine")
local Msg = require("core.Msg")

ITEM_HERO_SUIPIAN = ITEM_HERO_SUIPIAN or {}

-- 碎片缓存
function initAfterStart()
   for id, itemConfig in pairs(ItemExcel) do
      if itemConfig.mainType == ItemDefine.MAINTYPE_ITEM and
         itemConfig.subType == ItemDefine.ITEM_SUBTYPE_SUIPIAN then
         
         if itemConfig.get[1] ~= SuipianLogic.SUMMON_TYPE_TARGET and 
            itemConfig.get[2] == SuipianLogic.SUMMON_SUBTYPE_HERO then
             
             local camp = itemConfig.cond[1]
             local star = itemConfig.cond[2]
             if not ITEM_HERO_SUIPIAN[camp] or not ITEM_HERO_SUIPIAN[camp][star] then
                 ITEM_HERO_SUIPIAN[camp] = ITEM_HERO_SUIPIAN[camp] or {}
                 ITEM_HERO_SUIPIAN[camp][star] = id
             end
         end
      end
   end
end


-- 根据星级 是否精英抽选英雄 阵营
local tableHeroDrop
function getDropHeroTable(star, isElite, campTb)
	tableHeroDrop = {}

	for heroID, heroConfig in pairs(HeroExcel.hero) do
		if star == heroConfig.star then -- 星级
			if heroConfig.isElite >= isElite then -- 是否精英
				local isMatchCamp = nil
				for i = 1, #campTb do
					if campTb[i] == heroConfig.camp then
						isMatchCamp = true
						break
					end
				end
				
				if isMatchCamp == true then
					tableHeroDrop[#tableHeroDrop + 1] = heroID
				end
			end 
		end
	end	
end

-- 根据部位 等级 稀有度 阵营抽选神器
local tableShenqiDrop
function getDropEquipTable(pos, lv, rare, campTb)
	tableShenqiDrop = {}

	for equipID, equipConfig in pairs(EquipExcel.equip) do
		if equipConfig.type == 3 then
			if equipConfig.pos == pos then
				if equipConfig.level == lv then
					if equipConfig.rare == rare then
						local isMatchCamp = nil
						for i = 1, #campTb do
							if campTb[i] == equipConfig.camp then
								isMatchCamp = true
								break
							end
						end
					
						if isMatchCamp == true then
							tableShenqiDrop[#tableShenqiDrop + 1] = equipID
						end
					
					end
				end
			end
		end
	end
end


-- getWay
function queryHeroGetWay(human, id, camp, star)
    local itemConfig = ItemExcel[id]
	local msgRet = Msg.gc.GC_ITEM_SUIPIAN_GETWAR_BY_HERO_ID
    if itemConfig then
        Grid.makeItem(msgRet.net, id, 1)
    else
       if not ITEM_HERO_SUIPIAN[camp] then return end
       if not ITEM_HERO_SUIPIAN[camp][star] then return end
       local itemID = ITEM_HERO_SUIPIAN[camp][star]
       Grid.makeItem(msgRet.net, itemID, 1)
    end
	Msg.send(msgRet, human.fd)
end